from flask_wtf import FlaskForm
from werkzeug.utils import secure_filename
from wtforms import StringField,IntegerField,DateField,RadioField,SelectMultipleField
from wtforms.validators import DataRequired
from flask import Flask, render_template
from flask_wtf.file import FileField
import os
from flask_bootstrap import Bootstrap


app = Flask(__name__)
bootstrap = Bootstrap(app)
app.secret_key = 'abc'
app.config['UPLOAD_FOLDER'] = 'upload'


# class MyForm(FlaskForm):
#     name = StringField('name', validators=[DataRequired()])

# class MyForm(FlaskForm):
#     name = StringField(label='姓名')
#     city = StringField('城市')
#     birthday = DateField(label='生日', format="%Y-%m-%d", validators=[DataRequired('日期格式不正确')])
#     gender = RadioField(label='性别', choices=[(1, 'male'), (2, 'female')])
#     interest = SelectMultipleField(label='兴趣', choices=[(1, 'Football'), (2, 'Movies'), (3, 'Reading')])


class PhotoForm(FlaskForm):
  photo = FileField('上传照片')


# @app.route('/', methods=('GET', 'POST'))
# def submit():
#     form = MyForm()
#     if form.validate_on_submit():
#         return '成功！'
#     return render_template('submit.html', form=form)


@app.route('/upload', methods=['GET', 'POST'])
def upload():
    form = PhotoForm()
    filepath = None
    if form.validate_on_submit():
        if form.photo.data == None:
            return "上传文件是空文件"
        filename = secure_filename(form.photo.data.filename)
        file = form.photo.data
        filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
        file.save(filepath)
        return render_template('success.html', form=form, filename=filename)
    else:
        filename = None
    return render_template('upload.html', form=form, filename=filename)

'''
form表单是原生htnl、可以借助Bootstrap-flask 定义了很多模板宏，减少重复的编码
'''
if __name__ == '__main__':
  app.run(debug=True)